//
//  SISSRequestCooperationView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/4/21.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSRequestCooperationView.h"
#import "SISSTextView.h"


static CGFloat contentViewHeight = 280;

@interface SISSRequestCooperationView ()<UITextViewDelegate>

/** contentView */
@property (weak, nonatomic) SISSBaseView *contentView;
/** textView */
@property (weak, nonatomic) SISSTextView *textView;

@end
@implementation SISSRequestCooperationView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        
        [self setupUI];
    }
    return self;
}

- (void)setCooperationTextStr:(NSString *)cooperationTextStr {
    _cooperationTextStr = cooperationTextStr;
    self.textView.text = cooperationTextStr;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    
    CGRect rect = [self.contentView convertRect:self.contentView.bounds toView:self];
    if ((SCREEN_HEIGHT - rect.origin.y - contentViewHeight) < [SISSPublicSingleton shared].keyboardHeight) {
        
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self).offset(-([SISSPublicSingleton shared].keyboardHeight - SCREEN_HEIGHT * 0.5 + 0.5 * contentViewHeight));
        }];
        
    }
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(-40);
    }];
    self.cooperationTextStr = textView.text;
}

#pragma mark - UI搭建
- (void)setupUI {
    
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    [UIView animateWithDuration:0.1 animations:^{
        self.alpha = 1;
    }];
    
//    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHiddenView)];
//    [self addGestureRecognizer:tapGesture];
    
    //------- 弹窗主内容 -------//
    SISSBaseView *contentView = [[SISSBaseView alloc]init];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    self.contentView = contentView;
    
    // 合作理由
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = [UIFont boldSystemFontOfSize:15];
    titleLabel.textColor = MAIN_TEXT_COLOR;
    titleLabel.text = @"合作理由:";
    [contentView addSubview:titleLabel];
    
    // textView
    SISSTextView *textView = [[SISSTextView alloc] init];
    textView.layer.borderWidth = 1;
    textView.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    textView.text = self.cooperationTextStr;
    textView.textColor = MAIN_TEXT_COLOR;
    textView.font = [UIFont systemFontOfSize:16];
    textView.delegate = self;
    [contentView addSubview:textView];
    self.textView = textView;
    
    // 取消按钮
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    cancelButton.layer.borderWidth = 1;
    cancelButton.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [cancelButton addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:cancelButton];

    
    // 确定按钮
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
    [confirmButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    confirmButton.layer.borderWidth = 1;
    confirmButton.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [confirmButton addTarget:self action:@selector(confirmButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:confirmButton];
    
    
    // 自动布局
    CGFloat margin = 8.0;
    CGFloat textViewHeight = 160;
    CGFloat buttonHeight = 44;
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(-5 * margin);
        make.left.equalTo(self).offset(margin * 0.5);
        make.right.equalTo(self.mas_right).offset(- margin * 0.5);
        make.height.mas_equalTo(contentViewHeight);
    }];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView).offset(2 * margin);
        make.left.equalTo(contentView).offset(margin);
    }];
    
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.top.equalTo(titleLabel.mas_bottom).offset(margin);
        make.right.equalTo(contentView).offset(-margin);
        make.height.mas_equalTo(textViewHeight);
    }];
    
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textView);
        make.top.equalTo(textView.mas_bottom).offset(3 *margin);
        make.right.equalTo(contentView.mas_centerX).offset(-margin);
        make.height.mas_equalTo(buttonHeight);
    }];
    
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(textView.mas_right);
        make.top.equalTo(cancelButton);
        make.left.equalTo(contentView.mas_centerX ).offset(margin);
        make.height.mas_equalTo(buttonHeight);
    }];
    
    [self show];
}

/**
 确定按钮点击事件
 */
- (void)confirmButtonClicked {
    
    if ([self.delegate respondsToSelector:@selector(requestCooperationViewClickConfirmButtonWithCooperationReason:)]) {
        if (self.textView.text.length == 0) return;
        [self.delegate requestCooperationViewClickConfirmButtonWithCooperationReason:self.textView.text];
        [self dismiss];
    }
}

/**
 取消按钮点击事件
 */
- (void)cancelButtonClicked {
    
    [self dismiss];
}

#pragma mark - 弹出此弹窗

- (void)show{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
}

#pragma mark - 移除此弹窗
- (void)dismiss{
    
    [self removeFromSuperview];
}

- (void)tapGestureHiddenView {
    
    __weak typeof(self) weakSelf = self;
    
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf dismiss];
    }];
}

@end
